package org.algorithm.熊猫.链表;

import org.algorithm.biubiu.wheel_1.common.ListNode;
import org.junit.Test;

/**
 * @author
 * @Date 2022/2/22
 * @Link {https://leetcode-cn.com/problems/SLwz0R/}
 */
public class 删除链表的倒数第n个结点 {


    @Test
    public void test() {
        ListNode listNode = removeNthFromEnd(ListNode.build(1, 2), 2);
        System.out.println("listNode = " + listNode);
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode dyNode = new ListNode(0);
        dyNode.next = head;

        ListNode fast = dyNode;
        while (n-- > 0) {
            fast = fast.next;
        }

        ListNode last = dyNode;
        while (fast.next != null) {
            fast = fast.next;
            last = last.next;
        }
        last.next = last.next.next;
        return dyNode.next;
    }
}
